#include "widget.h"
#include "ui_widget.h"

#include <QLineEdit>
#include <QDebug>
#include <QRegExpValidator>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //设置按钮禁用
    ui->pushButton->setEnabled(false);
    //设置提示信息
    ui->lineEdit_name->setPlaceholderText("请输入姓名：");
    ui->lineEdit_phone->setPlaceholderText("请输入电话：");
    ui->lineEdit_password->setPlaceholderText("请输入密码：");
    ui->lineEdit_password_repeat->setPlaceholderText("请再次输入密码：");
    //设置密码模式
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    ui->lineEdit_password_repeat->setEchoMode(QLineEdit::Password);
    //设置清除的❌️号
    ui->lineEdit_name->setClearButtonEnabled(true);
    ui->lineEdit_phone->setClearButtonEnabled(true);
    ui->lineEdit_password->setClearButtonEnabled(true);
    ui->lineEdit_password_repeat->setClearButtonEnabled(true);
    //设置手机号格式
    //ui->lineEdit_phone->setInputMask("000-0000-0000");

    //设置正则表达式  QRegExpValidator 验证器对象  QRegExp 正则表达式
    ui->lineEdit_phone->setValidator(new QRegExpValidator(QRegExp("^1\\d{10}$")));

    //设置默认性别
    ui->radioButton_male->setChecked(true);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    qDebug()<<"姓名："<<ui->lineEdit_name->text()
           <<"密码"<<ui->lineEdit_password->text()
          <<"性别"<<(ui->radioButton_male->isChecked()?"男性":"女性")
         <<"电话"<<ui->lineEdit_phone->text();
}

void Widget::on_lineEdit_phone_textEdited(const QString &arg1)
{
    //qDebug()<<arg1;
    QString content=arg1;
    int pos=0;
    //判断
    if(ui->lineEdit_phone->validator()->validate(content,pos)==QValidator::Acceptable)
    {
        ui->pushButton->setEnabled(true);
    }
    else
    {
        ui->pushButton->setEnabled(false);
    }

}

void Widget::passwordInfo()
{
    const QString& s1 = ui->lineEdit_password->text();
    const QString& s2 = ui->lineEdit_password_repeat->text();
    if(s1.isEmpty()&&s2.isEmpty())
    {
        ui->label_info->setText("密码为空");
    }
    else if(s1==s2)
    {
        ui->label_info->setText("密码相同");
    }
    else
    {
        ui->label_info->setText("密码不相同");
    }
}

void Widget::on_lineEdit_password_textEdited(const QString &arg1)
{
    (void)arg1;
    passwordInfo();
}

void Widget::on_lineEdit_password_repeat_textEdited(const QString &arg1)
{
    (void)arg1;
    passwordInfo();
}


void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
        ui->lineEdit_password_repeat->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
        ui->lineEdit_password_repeat->setEchoMode(QLineEdit::Password);
    }
}
